//
//  FullMainViewController.m
//  tangshantongjiAPPFull
//
//  Created by yuntai on 2018/4/20.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "FullMainViewController.h"
#import "FullMainCollectionViewCell.h"
#import "FullMainHeadCollectionReusableView.h"
#import "FullMainModel.h"
#import "NewsListViewController.h"
#import "FullNormalListViewController.h"
#import "MainViewController.h"
#import "ZaiXianDiaoChaViewController.h"
#import "ZhuHuDiaoChaoViewController.h"
#import "CaiGouGuiXiaViewController.h"
#import "XiaoJiaDiaoChaViewController.h"

@interface FullMainViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) NSMutableArray *bannerarray;
@end

static NSString *fullMainCollectionCellID = @"fullMainCollectionCellID";
static NSString *fullMainCollectionHeadId = @"fullMainCollectionCellID";

@implementation FullMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.array = [NSMutableArray array];
    self.bannerarray = [NSMutableArray array];
    
    // 创建瀑布流对象,设置cell的尺寸和位置
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    CGSize size = [UIScreen mainScreen].bounds.size;
    layout.itemSize = size;
    // 设置cell之间的间距
    layout.minimumInteritemSpacing = 1;
    // 设置行距
    layout.minimumLineSpacing = 1;
    [layout setSectionInset:UIEdgeInsetsMake(15, 0, 15, 0)];
    layout.footerReferenceSize = CGSizeMake(size.width, 0);
    layout.headerReferenceSize = CGSizeMake(size.width, kScreenBoundWidth*0.6);
    layout.itemSize =
    CGSizeMake((kScreenBoundWidth-2)/3,100);
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    [self.collectionView registerNib:[UINib nibWithNibName:@"FullMainCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:fullMainCollectionCellID];
    [self.collectionView registerNib:[UINib nibWithNibName:@"FullMainHeadCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:fullMainCollectionHeadId];
    
    
    weakself(self)
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakself getDateFromServer];
    }];
    [self.collectionView.mj_header beginRefreshing];
    
    /**xmpp登录*/
    [self loginXmppServer];
}


#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    FullMainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:fullMainCollectionCellID forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    FullMainModel *md = self.array[indexPath.row];
    cell.nameLabel.text = md.name?:@"";
    [cell.imageIconView setContentMode:UIViewContentModeScaleAspectFill];
    switch (md.fullType) {
        case FullTongJiShuJu:
            cell.imageIconView.image = [UIImage imageNamed:@"tongjishuju"];
            break;
            
        case FullTongJiZhiShi:
            cell.imageIconView.image = [UIImage imageNamed:@"tongjizhishi"];
            break;
            
        case FullTongJiFaGui:
            cell.imageIconView.image = [UIImage imageNamed:@"tongjifagui"];
            break;
            
        case FullYongHuXuQiu:
            cell.imageIconView.image = [UIImage imageNamed:@"yonghuxuqiu"];
            break;
            
        case FullZaiXianDiaoCha:
            cell.imageIconView.image = [UIImage imageNamed:@"zaixiandiaocha"];
            break;
            
        case FullShengChanJiaGe:
            cell.imageIconView.image = [UIImage imageNamed:@"shengchanjiage"];
            break;
            
        case FullZhuHuDiaoCha:
            cell.imageIconView.image = [UIImage imageNamed:@"zhuhudiaocha"];
            break;
            
        case FullGuiXiaGongYe:
            cell.imageIconView.image = [UIImage imageNamed:@"guixiagongye"];
            break;
            
        case FullCaiGouJingLi:
            cell.imageIconView.image = [UIImage imageNamed:@"caigoujingli"];
            break;
            
        case FullXiaoJiaDiaoCha:
            cell.imageIconView.image = [UIImage imageNamed:@"xiaojiadiaocha"];
            break;
            
        default:
            
            break;
    }
    return cell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
        FullMainHeadCollectionReusableView *headeview = (FullMainHeadCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:fullMainCollectionHeadId forIndexPath:indexPath];
        if(self.bannerarray.count){
           __block NSMutableArray *temptitleArr= [NSMutableArray array];
           __block NSMutableArray *tempurlArr = [NSMutableArray array];
            
            [self.bannerarray enumerateObjectsUsingBlock:^(FullMainModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [temptitleArr addObject:obj.newsTitle?:@""];
                [tempurlArr addObject:obj.imagePath?:@""];
            }];
            weakself(self)
            [headeview configHeadViewWithArray:[NSArray arrayWithArray:tempurlArr] titles:[NSArray arrayWithArray:temptitleArr] seleIndex:^(NSInteger index) {
                
                NewsListViewController *vc = [[NewsListViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                vc.type = 16;
                vc.title = @"最新资讯";
                [weakself.navigationController pushViewController:vc animated:YES];
            }];
        }
        return headeview;
    }else{
        return nil;
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    FullMainModel *md = self.array[indexPath.row];
    if(md.fullType == FullTongJiShuJu || md.fullType == FullTongJiZhiShi || md.fullType == FullYongHuXuQiu){
        FullNormalListViewController *vc = [[FullNormalListViewController alloc]init];
        vc.type = md.fullType;
        vc.title = md.name;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (md.fullType == FullShengChanJiaGe){
        MainViewController *vc =  [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle] ] instantiateViewControllerWithIdentifier:@"MainViewController"];
        vc.title = md.name;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];

    }else if (md.fullType == FullTongJiFaGui){
        NewsListViewController *vc = [[NewsListViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.type = 19;
        vc.title = md.name;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (md.fullType == FullZaiXianDiaoCha){
        /**在线调查*/
        ZaiXianDiaoChaViewController *vc = [[ZaiXianDiaoChaViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.title = md.name;
        vc.type = ZaiXianDiaoCha;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (md.fullType == FullZhuHuDiaoCha){
        /**住户调查*/
        ZhuHuDiaoChaoViewController *vc = [[ZhuHuDiaoChaoViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.title = md.name;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (md.fullType == FullGuiXiaGongYe){
        /**规下工业*/
        CaiGouGuiXiaViewController *vc = [[CaiGouGuiXiaViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.title = md.name;
        vc.type = FullGuiXiaGongYe;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (md.fullType == FullCaiGouJingLi){
        /**采购经理*/
        CaiGouGuiXiaViewController *vc = [[CaiGouGuiXiaViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.title = md.name;
        vc.type = FullCaiGouJingLi;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (md.fullType == FullXiaoJiaDiaoCha){
        /**销价调查*/
        XiaoJiaDiaoChaViewController *vc = [[XiaoJiaDiaoChaViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}


#pragma mark - privatemethod
-(void)getDateFromServer{
    NSMutableDictionary *parma= [NSMutableDictionary dictionary];
    [parma setValue:kTokenID forKey:@"token"];
    
//    [HUDManager showLoadingHUDView:self.view];
    weakself(self)
    [NetWork PostNetWorkWithUrl:@"api/index/getMenuAndBaner.ajax" with:parma successBlock:^(NSDictionary *dic) {
        [weakself.collectionView.mj_header endRefreshing];
        weakself.bannerarray = [FullMainModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"tjyNews"]];
        weakself.array = [FullMainModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"menu"]];

        
        [weakself.collectionView reloadData];
        
    } FailureBlock:^(NSString *msg) {
        [weakself.collectionView.mj_header endRefreshing];

    } errorBlock:^(id error) {
        [weakself.collectionView.mj_header endRefreshing];

    }];
}
@end
